package com.company.demo04BSTCP;

import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

public class ServerThread extends Thread{
    private final Socket socket; // 用于接收主线程提交过来的客户端 socket对象
    public ServerThread(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        // 线程的任务逻辑代码：负责接收客户端的数据
        try {
            // 3. 获取字节输出流对象，用于响应浏览器的请求
            OutputStream os = socket.getOutputStream();
            // 4. 由于http协议中要求的响应数据格式需要换行，因此可以使用打印流包装低级的字节输入流对象
            PrintStream ps = new PrintStream(os);
            // 5. 获取浏览器的IP地址和端口号
            System.out.println("来自客户端IP为：" + socket.getInetAddress().getHostAddress() + "，端口号为：" + socket.getPort() + "的连接");
            // 6. 响应一个网页数据给浏览器
            ps.println("HTTP/1.1 200 OK");
            ps.println("Content-Type:text/html;charset=utf-8");
            ps.println();
            // 发送包含CSS爱心效果的HTML代码
            ps.println("<!DOCTYPE html>");
            ps.println("<html lang=\"zh\">");
            ps.println("<head>");
            ps.println("    <meta charset=\"UTF-8\">");
            ps.println("    <title>景纬宇给陈思的♥</title>");
            ps.println("    <style>");
            ps.println("        body {");
            ps.println("            display: flex;");
            ps.println("            justify-content: center;");
            ps.println("            align-items: center;");
            ps.println("            height: 100vh;");
            ps.println("            background-color: #f8f8f8;");
            ps.println("            margin: 0;");
            ps.println("        }");
            ps.println("        .heart {");
            ps.println("            position: relative;");
            ps.println("            width: 100px;");
            ps.println("            height: 100px;");
            ps.println("            background-color: red;");
            ps.println("            transform: rotate(-45deg);");
            ps.println("            animation: heartbeat 1s infinite;");
            ps.println("        }");
            ps.println("        .heart::before,");
            ps.println("        .heart::after {");
            ps.println("            content: '';");
            ps.println("            position: absolute;");
            ps.println("            width: 100px;");
            ps.println("            height: 100px;");
            ps.println("            background-color: red;");
            ps.println("            border-radius: 50%;");
            ps.println("        }");
            ps.println("        .heart::before {");
            ps.println("            top: -50px;");
            ps.println("            left: 0;");
            ps.println("        }");
            ps.println("        .heart::after {");
            ps.println("            left: 50px;");
            ps.println("            top: 0;");
            ps.println("        }");
            ps.println("        @keyframes heartbeat {");
            ps.println("            0%, 100% {");
            ps.println("                transform: scale(1) rotate(-45deg);");
            ps.println("            }");
            ps.println("            50% {");
            ps.println("                transform: scale(1.2) rotate(-45deg);");
            ps.println("            }");
            ps.println("        }");
            ps.println("    </style>");
            ps.println("</head>");
            ps.println("<body>");
            ps.println("    <div class=\"heart\"></div>");
            ps.println("</body>");
            ps.println("</html>");
            ps.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
